import router from '@/router/index'
import store from '@/store/index'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
const whliePage = ['/404', '/login']
router.beforeEach(async(to, from, next) => {
  // 假的进度条加深用户体验
  Nprogress.start()
  // 判断是否登陆
  if (store.state.user.token) {
    // 有token
    // 判断去得页面是否是否是白名单
    if (to.path.toLowerCase() === '/login') {
      next('/')
    } else {
      // 判断是否存在用户信息
      if (store.state.user.userInfo.userId) {
        next()
      } else {
        await store.dispatch('user/getUseInfo')
        next(to.fullPath)
      }
    }
  } else {
    // 无touken
    if (whliePage.includes(to.path.toLowerCase())) {
      next()
    } else {
      next('/login?redirect=' + to.fullPath)
    }
  }
  Nprogress.done()
})

router.afterEach((to, from) => {
  Nprogress.done()
})
